package com.yunqiao.api.controller;

import com.yunqiao.api.annotation.Login;
import com.yunqiao.common.exception.CustomApiException;
import com.yunqiao.common.utils.ApiResponse;
import com.yunqiao.common.utils.PageUtils;
import com.yunqiao.common.validator.ValidatorUtils;
import com.yunqiao.service.api.SysSmscodeService;
import com.yunqiao.service.api.entity.SysSmscodeEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;


/**
 * 短信验证码
 * @author Crocutax
 * @email wangxw725@163.com
 * @date 2019-03-06 15:30:51
 */
@RestController
@RequestMapping("api/syssmscode")
@Api(tags = "SysSmscodeApiController" ,description = "短信验证码管理")
public class SysSmscodeApiController {

    @Autowired
    private SysSmscodeService sysSmscodeService;

    /**
     * 列表
     */
    @ApiImplicitParams({
        @ApiImplicitParam(name = "page", value = "页码", required = false ,defaultValue = "1",paramType = "query" , dataType = "int"),
        @ApiImplicitParam(name = "limit", value = "数量", required = false,defaultValue = "10",paramType = "query" , dataType = "int")
    })
    @GetMapping("/list")
    @ApiOperation("查询短信验证码列表")
    public ApiResponse<PageUtils<SysSmscodeEntity>> list(@ApiIgnore @RequestParam Map<String, Object> params){
        PageUtils page = sysSmscodeService.queryPage(params);
        return new ApiResponse<>(page);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation("查询单个短信验证码")
    public ApiResponse<SysSmscodeEntity> info(@PathVariable("id") Integer id){
        if (id == null) {
            throw new CustomApiException("ID不能为空");
        }
        SysSmscodeEntity sysSmscode = sysSmscodeService.getById(id);
        return new ApiResponse<>(sysSmscode);
    }

    /**
     * 保存
     */
    @Login
    @PostMapping("/save")
    @ApiOperation("保存短信验证码")
    public ApiResponse save(SysSmscodeEntity sysSmscode){
        ValidatorUtils.validateEntity(sysSmscode);
        sysSmscodeService.save(sysSmscode);
        return new ApiResponse<>();
    }

    /**
     * 修改
     */
    @Login
    @PutMapping("/update/{id}")
    @ApiOperation("修改短信验证码")
    public ApiResponse update(@PathVariable Integer id , SysSmscodeEntity sysSmscode){
        SysSmscodeEntity dbItem = sysSmscodeService.getById(id);
        if(dbItem==null){
            throw new CustomApiException("ID有误, 未找到对应数据");
        }
        sysSmscodeService.updateById(sysSmscode);
        return new ApiResponse<>();
    }

    /**
     * 删除
     */
    @Login
    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除短信验证码")
    public ApiResponse delete(@PathVariable Integer id){
        if (id == null) {
            throw new CustomApiException("ID不能为空");
        }
        sysSmscodeService.removeById(id);
        return new ApiResponse<>();
    }


    /**
     * 发送验证码
     */
    @PostMapping("/sendSms/{phone}")
    @ApiOperation("发送验证码")
    public ApiResponse sendSms(@PathVariable String phone) throws Exception {
        if (phone == null) {
            throw new CustomApiException("手机号不能为空");
        }
        sysSmscodeService.sendSms(phone);
        return new ApiResponse<>();
    }

}
